Configuring PicketBox
To configure this store you need to use the Configuration API to define some specific configuration.
Configuring the JPA-based Identity Store
ConfigurationBuilder builder = new ConfigurationBuilder();
// configure the JPA identity store
builder.identityManager().jpaStore();
PicketBoxManager picketBoxManager = // create and start the manager
IdentityManager identityManager = picketBoxManager.getIdentityManager();
// now you can play with the Identity Manager instance using the JPA Identity Store.
You may notice that we did not specify any specific configuration about how the configured store should get a valid JPA EntityManager instance, join your transactions, etc.
Using the EntityManagerPropagationContext to provide a EntityManager instance
When using this store you only need to provide the JPA EntityManager using a ThreadLocal by using the org.picketbox.core.identity.jpa.EntityManagerPropagationContext class.
Here is a complete example that demonstrates how to configure and use this store using a JUnit test case.
private EntityManagerFactory entityManagerFactory;
/**
* <p>
* Tests if the authentication performs successfully when provided a valid {@link UsernamePasswordCredential}.
* </p>
*
* @throws AuthenticationException
*/
@Test
public void testUserNamePasswordCredential() throws AuthenticationException {
ConfigurationBuilder builder = new ConfigurationBuilder();
// configure the JPA identity store
builder.identityManager().jpaStore();
PicketBoxManager picketBoxManager = createManager(builder);
UserContext authenticatingUser = new UserContext();
authenticatingUser.setCredential(new UsernamePasswordCredential("admin", "admin"));
// let's authenticate the user
UserContext authenticatedUser = picketBoxManager.authenticate(authenticatingUser);
assertNotNull(authenticatedUser);
assertTrue(authenticatedUser.isAuthenticated());
assertRoles(authenticatedUser);
assertGroups(authenticatedUser);
}
@Before
public void onSetup() throws Exception {
this.entityManagerFactory = Persistence.createEntityManagerFactory("picketbox-testing-pu");
EntityManager entityManager = this.entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// sets the current JPA EntityManager instance
EntityManagerPropagationContext.set(entityManager);
}
@After
public void onFinish() throws Exception {
EntityManager entityManager = EntityManagerPropagationContext.get();
entityManager.flush();
entityManager.getTransaction().commit();
entityManager.close();
// clear the context. Do not forget to do that.
EntityManagerPropagationContext.clear();
this.entityManagerFactory.close();
}
The example above uses the onSetup() and onFinish() methods to populate and clear the EntityManagerPropagationContext, respectively.
Usually you'll put the EntityManagerPropagationContext logic inside a Servlet Filter (if you're configuring a web application) or using some kind of interceptor component like the ones provided by CDI or the EJB specifications.
Implementing different strategies to lookup the EntityManager
If you do not want use the EntityManagerPropagationContext with its ThreadLocal strategy to lookup for the EntityManager instance, you can provide a custom implementation for the org.picketbox.core.identity.jpa.EntityManagerLookupStrategy class.
The example bellow demonstrates how a implementation looks like when trying to provide a EntityManager instance in a CDI environment.
@ApplicationScoped
public class CDIEntityManagerLookupStrategy extends EntityManagerLookupStrategy {
@Inject
private Instance<EntityManager> entityManager;
@Override
protected EntityManager lookupEntityManager() {
EntityManager entityManager = null;
try {
entityManager = this.entityManager.get();
} catch (Exception e) {
// handle when no entitymanager instance is found
}
return entityManager;
}
}
To use your custom implementation you only need to specify it when building the PicketBox configuration.
@Inject
private CDIEntityManagerLookupStrategy entityManagerLookupStrategy;
@Produces
public ConfigurationBuilder createConfiguration() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder
.identityManager()
.jpaStore()
.entityManagerLookupStrategy(this.entityManagerLookupStrategy);
return builder;
}